/*
	Flie:						CTempFont.cpp
	Course:				SGP 1208
	Author:				Kori Po
	Purpose:			Temporary cool looking font
*/

#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "TempFont.h"


CTempFont::CTempFont(void)
{
	m_TextureGuy = nullptr;

	m_TextureGuy = CSGD_TextureManager::GetInstance();

	m_nImageID = m_TextureGuy->LoadTexture(_T("Resource/KP_RuneFont.bmp"), D3DCOLOR_XRGB(255,255,255));

	m_nCellWidth = 32;
	m_nCellHeight = 32;
	
	m_nNumCols = 8;
	m_nNumRows = 8;

	m_nCharHeight = 32;
	m_nCharWidth = 32;

	m_cFirstChar = ' ';
}


CTempFont::~CTempFont(void)
{
	m_TextureGuy->UnloadTexture(m_nImageID);
	m_TextureGuy = nullptr;
}

RECT CTempFont::CellAlgorithm( int id )
{
	assert( id >= 0 && id < m_nNumCols*m_nNumRows );

	RECT rSource = { };

	rSource.left = (id % m_nNumCols) * m_nCellWidth;
	rSource.top = ((id /m_nNumCols) * m_nCellHeight);

	rSource.right = rSource.left + m_nCellWidth;
	rSource.bottom = rSource.top + m_nCellHeight;

	return rSource;
}

void CTempFont::Print(	const char* szText,	 int nX, int nY, float fScale = 1.0f, DWORD dwColor = -1UL)
{
	// Keep track of the starting x position (for newlines)
		int nColStart = nX;

		// Iterate through the string 1 char at a time
		int len = strlen( szText );
		for( int i = 0; i < len; i++ )
		{
			// Get the char out of the string
			char ch = szText[ i ];

			// Handle whitespace
			if( ch == ' ' )
			{
				nX += m_nCharWidth * fScale;
				continue;
			}
			else if( ch == '\n' )
			{
				nY += m_nCharHeight * fScale;
				nX =  nColStart;
				continue;
			}

			// Convert to uppercase?
			if( m_bOnlyUppercase == true )
				ch = toupper( ch );


			// Calculate the tile id (relying on ASCII order!)
			int id = ch - m_cFirstChar;

			// Get source rect
			RECT rSource = CellAlgorithm( id );

			// Draw!
			m_TextureGuy->Draw( m_nImageID, nX, nY, fScale, fScale, &rSource, 0.0f, 0.0f, 0.0f, dwColor );

			// Move to the next position on screen
			nX += m_nCharWidth * fScale;
		}
};
	

